<?php
require_once("../../Config.php");
require_once("../../Class/ClassFunc.php");
require_once("../../Class/ClassMysql.php");
require_once("../../Class/ClassCheck.php");
require_once("../../Class/ClassResult.php");

//json头
header("Content-type: application/json");

session_start();
if(!isset($_SESSION["name"])) {
    exit(Result::jsonReturn(1, "未登录"));
}

$uid = $_SESSION["id"];
$uname = $_SESSION["name"];
$uemail = $_SESSION["email"];
$upower = $_SESSION["power"];


// 校验是否为空
if(Check::isEmpty($_POST["name"])) {
    exit(Result::jsonReturn(1, "名称不得为空"));
}
if(Check::isEmpty($_POST["slug"])) {
    exit(Result::jsonReturn(1, "简略名不得为空"));
}
if(Check::isEmpty($_POST["description"])) {
    exit(Result::jsonReturn(1, "介绍不得为空"));
}

$info = array();
// 校验参数合法性
if(Check::isName($_POST["name"])) {
    $info["name"] = $_POST["name"];
}else {
    exit(Result::jsonReturn(1, "名称不符合规定"));
}
if(Check::isSlug($_POST["slug"])) {
    $info["slug"] = $_POST["slug"];
}else {
    exit(Result::jsonReturn(1, "简略名只能为英文字母、数字、下划线"));
}
if(!Check::isEmpty($_POST["type"]) && Check::isNum($_POST["type"]) && (int)$_POST["type"]==1) {
    $info["type"] = 1;
}else {
    $info["type"] = 0;
}
$info["description"] = Check::checkPost($_POST["description"]);

// 构造sql语句
$db = new Db();
$sql = "INSERT INTO qilian_lists(user_id, user_name, name, slug, description, type)".
        " VALUES({$uid}, '{$uname}', '{$info["name"]}', '{$info["slug"]}', '{$info["description"]}', {$info["type"]})";
$id = 0;
if($id=$db->insert($sql)) {
    exit(Result::jsonReturn(0, "添加分类成功", array(
        "id" => $id,
        "user_id" => $uid,
        "user_name" => $uname,
        "name" => $info["name"],
        "slug" => $info["slug"],
        "description" => $info["description"],
        "type" => $info["type"])));
}else {
    exit(Result::jsonReturn(1, "添加分类失败"));
}



?>